package com.yjt.zdjf.standard.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.yjt.common.util.TimeUtil;
import com.yjt.zdjf.standard.common.constant.ConstantValues;
import com.yjt.zdjf.standard.model.zdcx.ZDCXReqMessage;

public class TcpOrgClient {
	
	public static void main(String[] args) throws InterruptedException {
		Socket client;
		ZDJFSerializerDeserializerForTest tool = new ZDJFSerializerDeserializerForTest();
		try {
			client = new Socket("127.0.0.1", 1729);
			InputStream is = client.getInputStream();
			OutputStream os = client.getOutputStream();
			ZDCXReqMessage message = new ZDCXReqMessage();
			message.getBody().setByzd("备用字段");
			message.getBody().setJfh("缴费号");
			message.getBody().setJszdzq("结算账单周期");
			message.getBody().setQszdzq("清算账单周期");
			message.getBody().setQyh("签约");
			message.getHead().setOuterMsgId("12344");
			message.getHead().setRecSysId("0001");
			message.getHead().setSendDate(TimeUtil.getCurDate("yyyyMMdd"));
			message.getHead().setSendTime(TimeUtil.getCurDate("HHmmss"));
			message.getHead().setTxnType("zdcx");
			
			String request = JSON.toJSONString(message);
			//起始字节符
			os.write(ConstantValues.STX);
			//8位报文长度，十进制表示，不足左补零
			os.write(StringUtils.leftPad(String.valueOf(request.getBytes("utf-8").length), ConstantValues.MESSAGE_LENGTH_DIGIT, '0').getBytes());
			//报文体
			os.write(request.getBytes(ConstantValues.MESSAGE_CHARSET));
			//结束字节符
			os.write(ConstantValues.ETX);
			os.flush();
			//client.shutdownOutput();
			System.out.println("isShutdownInputStram = " + client.isInputShutdown());
			System.out.println("isClosed = " + client.isClosed());
			System.out.println("isConnected = " + client.isConnected());
			
			System.out.println("org client receive : " + tool.inputStreamToString(is));
			
			client.shutdownOutput();
			client.shutdownInput();
			client.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
